package com.ruoyi.member.controller;

import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.file.FileUtils;
import com.ruoyi.file.controller.SysFileController;
import com.ruoyi.file.utils.FileUploadUtils;
import com.ruoyi.system.api.RemoteFileService;
import com.ruoyi.system.api.domain.SysFile;
import com.ruoyi.system.api.factory.RemoteFileFallbackFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.member.domain.Member;
import com.ruoyi.member.service.IMemberService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 商家信息Controller
 * 
 * @author ruoyi-bai
 * @date 2023-08-02
 */
@RestController
@RequestMapping("/member")
public class MemberController extends BaseController
{

//    private static final Logger log = LoggerFactory.getLogger(SysFileController.class);
    @Autowired
    private IMemberService memberService;

    /**
     * 文件上传请求
     */
    @PostMapping("/upload")
    public Map<String,Object> upload(MultipartFile file)
    {
        return memberService.uploadFile(file);
    }

//    /**
//     * 文件上传请求
//     */
//    @PostMapping("/upload")
//    public R<SysFile> upload(MultipartFile file)
//    {
//        try
//        {
//            // 上传并返回访问地址
//            String url = memberService.uploadFile(file);
//            SysFile sysFile = new SysFile();
//            sysFile.setName(FileUtils.getName(url));
//            sysFile.setUrl(url);
//            return R.ok(sysFile);
//        }
//        catch (Exception e)
//        {
//            log.error("上传文件失败", e);
//            return R.fail(e.getMessage());
//        }
//    }



    /**
     * 查询商家信息列表
     */
    @RequiresPermissions("member:member:list")
    @GetMapping("/list")
    public TableDataInfo list(Member member)
    {
        startPage();
        List<Member> list = memberService.selectMemberList(member);
        return getDataTable(list);
    }

    /**
     * 导出商家信息列表
     */
    @RequiresPermissions("member:member:export")
    @Log(title = "#商家信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Member member)
    {
        List<Member> list = memberService.selectMemberList(member);
        ExcelUtil<Member> util = new ExcelUtil<Member>(Member.class);
        util.exportExcel(response, list, "#商家信息数据");
    }

    /**
     * 获取#商家信息详细信息
     */
    @RequiresPermissions("member:member:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(memberService.selectMemberById(id));
    }

    /**
     * 新增#商家信息
     */
    @RequiresPermissions("member:member:add")
    @Log(title = "#商家信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Member member)
    {
        return toAjax(memberService.insertMember(member));
    }

    /**
     * 修改#商家信息
     */
    @RequiresPermissions("member:member:edit")
    @Log(title = "#商家信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Member member)
    {
        return toAjax(memberService.updateMember(member));
    }

    /**
     * 删除#商家信息
     */
    @RequiresPermissions("member:member:remove")
    @Log(title = "#商家信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(memberService.deleteMemberByIds(ids));
    }
}
